package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.OrderShipments;
import com.example.mall.dto.OrderShipmentsDto;
import com.example.mall.mapper.OrderShipmentsMapper;
import com.example.mall.query.OrderShipmentsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IOrderShipmentsService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单物流表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class OrderShipmentsServiceImpl extends ServiceImpl<OrderShipmentsMapper, OrderShipments> implements IOrderShipmentsService {

    @Autowired
    private OrderShipmentsMapper orderShipmentsMapper;

    @Override
    public PageDTO<OrderShipmentsDto> queryPage(OrderShipmentsQuery orderShipmentsQuery) {
        if (orderShipmentsQuery == null) {
            orderShipmentsQuery = new OrderShipmentsQuery();
        }
        Page<OrderShipments> page = orderShipmentsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<OrderShipments> cartPage = lambdaQuery()
                .like(orderShipmentsQuery.getShipmentId()!= null, OrderShipments::getShipmentId, orderShipmentsQuery.getShipmentId())
                .like(orderShipmentsQuery.getOrderId()!= null, OrderShipments::getOrderId, orderShipmentsQuery.getOrderId())
                .like(orderShipmentsQuery.getCarrier()!= null, OrderShipments::getCarrier, orderShipmentsQuery.getCarrier())
                .like(orderShipmentsQuery.getStatus()!= null, OrderShipments::getStatus, orderShipmentsQuery.getStatus())
                .like(orderShipmentsQuery.getTrackingNumber()!= null, OrderShipments::getTrackingNumber, orderShipmentsQuery.getTrackingNumber())
                .orderByDesc(OrderShipments::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, OrderShipmentsDto.class);
    }

    @Override
    public OrderShipments queryOrderShipmentsById(Long shipmentId) {
        AssertUtil.isNotNull(shipmentId, "订单物流ID不能为空");
        return orderShipmentsMapper.selectById(shipmentId);
    }

    @Override
    public List<OrderShipments> queryOrderShipmentsList() {
        return this.list();
    }

    @Override
    public void update(OrderShipmentsDto orderShipmentsDto) {
        AssertUtil.isNotNull(orderShipmentsDto.getShipmentId(), "订单物流ID不能为空");
        OrderShipments orderShipments = this.queryOrderShipmentsById(orderShipmentsDto.getShipmentId());
        AssertUtil.isNotNull(orderShipments, "订单物流不存在");
        CopyUtils.copyProperties(orderShipmentsDto, orderShipments);
        orderShipments.setUpdateDate(new Date());
        this.updateById(orderShipments);
    }

    @Override
    public void delete(Long shipmentId) {
        AssertUtil.isNotNull(shipmentId, "订单物流ID不能为空");
        OrderShipments orderShipments = orderShipmentsMapper.selectById(shipmentId);
        AssertUtil.isNotNull(orderShipments, "订单物流不存在");
        orderShipments.setUpdateDate(new Date());
        orderShipments.setStatus(BaseConstants.CANCEL);
        orderShipmentsMapper.updateById(orderShipments);
    }

    @Override
    public void deleteBatch(List<Long> shipmentIds) {
        AssertUtil.isNotNull(shipmentIds, "订单物流ID列表不能为空");
        List<OrderShipments> orderShipments = orderShipmentsMapper.selectBatchIds(shipmentIds);
        orderShipments.forEach(orderShipments1 -> {
            orderShipments1.setUpdateDate(new Date());
            orderShipments1.setStatus(BaseConstants.CANCEL);
            orderShipmentsMapper.updateById(orderShipments1);
        });
    }

    @Override
    public void insert(OrderShipmentsDto orderShipmentsDto) {
        OrderShipments orderShipments = new OrderShipments();
        CopyUtils.copyProperties(orderShipmentsDto, orderShipments);
        orderShipments.setShipmentId(CodeGenerateUtils.generateUniqueId());
        orderShipments.setCreateDate(new Date());
        orderShipments.setUpdateDate(new Date());
        orderShipments.setStatus(BaseConstants.NOT_USED);
        orderShipmentsMapper.insert(orderShipments);
    }
}
